package com.itheima.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.bean.Dish;
import com.itheima.Dto.DishDto;
import com.itheima.VO.IdStaVO;
import com.itheima.common.PageParm;
import com.itheima.common.R;
import com.itheima.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;


    /**
     * 添加菜品
     *
     * @param dishDto
     * @return
     */
    @PostMapping
    public R addDish(@RequestBody DishDto dishDto) {

        log.info(dishDto.toString());
        int row = dishService.saveWithFlavor(dishDto);

        if (row > 0) {
            return R.success("新增菜品成功！！！");
        } else return R.error("添加菜品失败！！！");

    }

    /**
     * 分页查询
     *
     * @param param
     * @return
     */
    @GetMapping("/page")
    public R selectPage(PageParm param) {
        IPage<DishDto> dishIDtoPage = dishService.selectPage(param);
        return R.success(dishIDtoPage);
    }

    /**
     * 根据菜品ID查询菜品所有信息
     *
     * @param id
     * @return
     */
    @GetMapping
    public R selectById(long id) {
        Dish dish = dishService.selectByIdToDto(id);
        return R.success(dish);
    }

    /**
     * 修改菜品
     *
     * @param dishDto
     * @return
     */
    @PutMapping
    public R update(@RequestBody DishDto dishDto) {
        int row = dishService.update(dishDto);
        if (row > 0)
            return R.success("更新菜品成功！！！");
        else return R.error("更新菜品失败！！！");
    }


    /**
     * 更新菜品状态信息
     *
     * @param idStaVO
     * @return
     */
    @PostMapping("/status")
    public R upStatus(@RequestBody IdStaVO idStaVO) {

        int update = dishService.upStatus(idStaVO);
        System.out.println(update);
        if (update > 0)
            return R.success("修改状态成功！！！");
        else return R.error("修改状态失败！！！");
    }

    /**
     * 添加套餐查询菜品分类ID的菜品信息
     *
     * @return
     */
    @GetMapping("/list")
    public R selectList(long categoryId) {
        List<Dish> dishes = dishService.selectByCategoryID(categoryId);
        return R.success(dishes);
    }

    /**
     * 批量删除菜品所有信息
     * @param ids
     * @return
     */
    @DeleteMapping
    public R deleteByIds( @RequestParam List<Long> ids){
        int row = dishService.deleteByIds(ids);
        if(row>0)
            return R.success("批量删除套餐成功！！！");
        else return R.error("批量删除失败！！！");
    }
    @GetMapping("/lists")
    public R selectListUse(long categoryId) {
        List<DishDto> dishes = dishService.selectByCategoryIDUse(categoryId);

        return R.success(dishes);
    }

}
